package 综合应用.T2;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class T2 {

    public static void main(String[] args) throws Exception {
        Map<String, String> map1 = new HashMap<>();
        Map<String, String> map2 = new HashMap<>();

        SAXReader sr = new SAXReader();
        Document doc = sr.read("综合应用/T2/web.xml");
        Element rootElement = doc.getRootElement();
        List<Element> elements = rootElement.elements("servlet");
        for (Element element : elements) {
            String name = element.elementText("servlet-name");
            String sclass = element.elementText("servlet-class");
            map1.put(name, sclass);
        }

        List<Element> elementse = rootElement.elements("servlet-mapping");
        for (Element element : elementse) {
            String name = element.elementText("servlet-name");
            String url = element.elementText("url-pattern");
            map2.put(name,url);
        }

        Map<String,Servlet> map = new HashMap<>();
        for (String s : map1.keySet()) {
            String bigMapKey = map2.get(s);
            String className = map1.get(s);
            Class<?> aClass = Class.forName(className);
            Object obj = aClass.newInstance();
            map.put(bigMapKey,(Servlet)obj);
        }

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个路径");
        String ss = sc.nextLine();

        for (String s : map.keySet()) {
            if (ss.equals(s)){
                Servlet servlet = map.get(ss);
                servlet.doGet();
            }
        }

    }
}
